package com.djzhou.gmms.company.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.djzhou.gmms.base.action.BaseAction;
import com.djzhou.gmms.model.company.TbCompany;
import com.opensymphony.xwork2.ModelDriven;

public class CompanyAction extends BaseAction<TbCompany> implements ModelDriven<TbCompany> {

	private TbCompany company;
	private List<TbCompany> list;
	private Integer companyId;
	
	private String companyNo;
	private String companyName;
	
	private List<TbCompany> motherCompanyList;

	@Override
	public String delete() {
		setCompanyName(companyName);
		setCompanyNo(companyNo);
		companyService.delete(companyId);
		msg="删除成功";
		setCurrentPage(currentPage);
		logService.addLog("公司管理",queryUser(),"删除",companyId,"TbCompany",super.queryCompany(),super.queryDepartment());
		return SUCCESS;
	}

	@Override
	public String modify() {
		setCompanyName(companyName);
		setCompanyNo(companyNo);
		company.setLastModifyTime(new Date());
		
		if(company.getParentCompanyId()!=0){
			company.setChildCompanyFlag(1);
			String companyPath=",";
			TbCompany parentCompany=company;
			for(;;){
				parentCompany=companyService.queryById(parentCompany.getParentCompanyId());//获取父级对象
				companyPath+=parentCompany.getCompanyId()+",";
				if(parentCompany.getParentCompanyId()==0)break;
			}
			company.setCompanyPath(companyPath);
		}
		else{
			 company.setChildCompanyFlag(0);
			 company.setCompanyPath(",0,");
		}
		
		company.setUser(queryUser());
		companyService.update(company);
		msg="修改成功";
		setCurrentPage(currentPage);
		logService.addLog("公司管理",queryUser(),"编辑",company.getCompanyId(),"TbCompany",super.queryCompany(),super.queryDepartment());
		return SUCCESS;
	}

	@Override
	public String queryList() {
		if(loadData==null)return SUCCESS;
		HashMap<String, Object> hashMap=new HashMap<String, Object>();
		if(companyNo!=null && !companyNo.equals(""))hashMap.put("companyNo", companyNo);
		if(companyName!=null && !companyName.equals(""))hashMap.put("companyName", companyName);
		hashMap.put("companyId", queryCompany().getCompanyId());
		pageBean=companyService.queryPageBean(hashMap, pageSize, currentPage);
		list=pageBean.getList();
		if(currentPage>1){
			if(list==null || list.size()==0){
				setCurrentPage(getCurrentPage()-1);
				pageBean = companyService.queryPageBean(hashMap, pageSize, currentPage);
				list=pageBean.getList();
			}
		}
		if(list!=null){
			totalCount=pageBean.getAllRow();
		}else{
			totalCount = 0;
			list = new ArrayList<TbCompany>();
		}
		totalPage=pageBean.getTotalPage();
		return SUCCESS;
	}

	@Override
	public String save() {
		setCompanyName(companyName);
		setCompanyNo(companyNo);
		if(company!=null){
			Date date=new Date();
			company.setCreateTime(date);
			company.setLastModifyTime(date);
			company.setDeleteFlag(0);
			company.setUser(queryUser());
			if(company.getParentCompanyId()!=0){
				company.setChildCompanyFlag(1);
				String companyPath=",";
				TbCompany parentCompany=company;
				for(;;){
					parentCompany=companyService.queryById(parentCompany.getParentCompanyId());//获取父级对象
					companyPath+=parentCompany.getCompanyId()+",";
					if(parentCompany.getParentCompanyId()==0)break;
				}
				company.setCompanyPath(companyPath);
			}
			else{
				 company.setChildCompanyFlag(0);
				 company.setCompanyPath(",0,");
			}
			
			
			companyService.add(company);
		}
		setCurrentPage(currentPage);
		logService.addLog("公司管理",queryUser(),"添加",company.getCompanyId(),"TbCompany",queryCompany(),super.queryDepartment());
		return SUCCESS;
	}

	@Override
	public String toAdd() {
		motherCompanyList=companyService.queryList(1);
		setCompanyName(companyName);
		setCompanyNo(companyNo);
		return SUCCESS;
	}

	@Override
	public String toEdit() {
		motherCompanyList=companyService.queryList(companyId);
		setCompanyName(companyName);
		setCompanyNo(companyNo);
		setCurrentPage(currentPage);
		company=companyService.queryById(companyId);
		return SUCCESS;
	}

	public TbCompany getModel() {
		return company;
	}

	public TbCompany getCompany() {
		return company;
	}

	public void setCompany(TbCompany company) {
		this.company = company;
	}

	public List<TbCompany> getList() {
		return list;
	}

	public void setList(List<TbCompany> list) {
		this.list = list;
	}

	public Integer getCompanyId() {
		return companyId;
	}

	public void setCompanyId(Integer companyId) {
		this.companyId = companyId;
	}

	public String getCompanyNo() {
		return companyNo;
	}

	public void setCompanyNo(String companyNo) {
		this.companyNo = companyNo;
	}

	public String getCompanyName() {
		return companyName;
	}

	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}
	
	
	public List<TbCompany> getMotherCompanyList() {
		return motherCompanyList;
	}

	public void setMotherCompanyList(List<TbCompany> motherCompanyList) {
		this.motherCompanyList = motherCompanyList;
	}

}
